<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/common-head.jsp"%>

<body>

    <!-- begin #content -->
    <div id="content" class="content">

        <!-- begin row -->
        <div class="row">
            <!-- begin col-6 -->
            <div class="col-xl-6">
                <!-- begin panel -->
                <div class="panel panel-inverse" data-sortable-id="form-stuff-11">
                    <!-- begin panel-heading -->
                    <div class="panel-heading">
                        <h4 class="panel-title">编辑用户信息</h4>
                        <div class="panel-heading-btn">
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-default" data-click="panel-expand"><i class="fa fa-expand"></i></a>
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-success" data-click="panel-reload"><i class="fa fa-redo"></i></a>
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-warning" data-click="panel-collapse"><i class="fa fa-minus"></i></a>
                            <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-danger" data-click="panel-remove"><i class="fa fa-times"></i></a>
                        </div>
                    </div>
                    <!-- end panel-heading -->
                    <!-- begin panel-body -->
                    <div class="panel-body">
                        <form action="/" method="POST">
                            <input type="hidden" id="id" value="${user.id}">
                            <input type="hidden" id="resId" value="${user.resId}" >

                            <fieldset>
                                <div class="form-group row m-b-15">
                                    <label class="col-md-3 col-form-label">头像</label>
                                    <div class="col-md-7">
                                        <img src="${cdnServer}${user.photo}" alt="这里是图片" id="userId" onclick="openPhoto()" width="114px">
                                        <input type="file" name="file" id="file" style="display: none" onchange="uploadFile()">
                                    </div>
                                </div>
                                <div class="form-group row m-b-15">
                                    <label class="col-md-3 col-form-label">用户名</label>
                                    <div class="col-md-7">
                                        <input type="email" class="form-control" placeholder="用户名" name="userName" id="userName" value="${user.userName}" />
                                    </div>
                                </div>
                                <div class="form-group row m-b-15">
                                    <label class="col-md-3 col-form-label">密码</label>
                                    <div class="col-md-7">
                                        <input type="password" class="form-control" placeholder="密码" name="password" id="password" value="${user.password}"/>
                                    </div>
                                </div>

                                <div class="form-group row">
                                    <div class="col-md-7 offset-md-3">
                                        <button type="button" class="btn btn-sm btn-primary m-r-5" id="doEditProfile">提交</button>
                                        <button type="button" class="btn btn-sm btn-default" onclick="back()">返回</button>
                                    </div>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                    <!-- end panel-body -->

                </div>
                <!-- end panel -->
            </div>
            <!-- end col-6 -->
        </div>
        <!-- end row -->

    </div>
    <!-- end #content -->

<%@include file="../common/common-script.jsp"%>

<script>
    var ctx = "${basePath}"

    var cdnServer = "${cdnServer}"
    function openPhoto(){
        $("#file").trigger("click")
    }

    function back(){
        window.location.href=ctx+'settings/profile'
    }

    function uploadFile(p){
        var formData=new FormData();
        formData.append("file",$('#file').prop('files')[0]);
        $.ajax({
            type:'POST',
            url:'/ServletUpload',
            data:formData,
            contentType: false,// 注意：让jQuery不要处理数据
            processData: false,// 注意：让jQuery不要设置contentType
            dataType:'json',
            success:function (res) {
                $("#userId").attr("src",cdnServer+res.path)
                $("#resId").val(res.id)
            }
        });
    }

    $(function(){
        $("#doEditProfile").click(function(){
            var id = $("#id").val()
            var userName = $("#userName").val()
            var password = $("#password").val()

            var resId = $("#resId").val()

            $.ajax({
                type:'post',
                url:ctx+'settings/editProfile',
                data:JSON.stringify({"id":id,"userName":userName,"password":password,"resId":resId}),
                success:function(res){
                    if(res=="1000"){
                        window.location.href=ctx+'settings/profile'
                    }else if (res =="1001"){
                        swal({
                            title: '错误！',
                            text: "发生异常，请重新设置",
                            icon: 'error',
                            buttons: {
                                cancel: {
                                    text: 'Cancel',
                                    value: null,
                                    visible: true,
                                    className: 'btn btn-default',
                                    closeModal: true,
                                },
                                confirm: {
                                    text: 'Warning',
                                    value: true,
                                    visible: true,
                                    className: 'btn btn-danger',
                                    closeModal: true
                                }
                            }
                        });
                    }else{
                        swal({
                            title: '错误！',
                            text: "出现异常，请联系管理员！",
                            icon: 'error',
                            buttons: {
                                cancel: {
                                    text: 'Cancel',
                                    value: null,
                                    visible: true,
                                    className: 'btn btn-default',
                                    closeModal: true,
                                },
                                confirm: {
                                    text: 'Warning',
                                    value: true,
                                    visible: true,
                                    className: 'btn btn-danger',
                                    closeModal: true
                                }
                            }
                        });
                    }
                }
            })

        })
    })
</script>

</body>
</html>